<?php
namespace Admin\Controller;

class SysTableController extends BaseController {
	public function index()
	{
        $this->import_title = 'Import';
        $this->setLangtable('sys_langtable');

		$this->display();
	}

    public function getData()
    {
        $module = M('SysLangtable');
        $data = $module->order('id')->select();
        $this->ajaxReturn($data, 'json');
    }

    public function getPageData()
    {
        $page = $_POST[page];
        $rows = $_POST[rows];
        if(!$page) $page = 1;
        if(!$rows) $rows = 20;
        $start = ($page - 1) * $rows;
        $end = $page * $rows;

        $module = M('SysLangtable');
        if($_POST[parameters]){
            $where = parent::handlerQueryparameters($_POST[parameters]);
            $count = $module->where($where)->count();
            $data = $module->where($where)->order('id')->limit($start, $end)->select();
        }else{
            $count = $module->count();
            $data = $module->order('id')->limit($start, $end)->select();
        }
        $page_data = array(
            'total' => $count,
            'rows' => $data
        );
        $this->ajaxReturn($page_data, 'json');
    }

    public function update()
    {
        $module = M('SysLangtable');

        if(isset($_POST[inserted])){
            $this->add($module, json_decode($_POST[inserted],true));
        }
        if(isset($_POST[updated])){
            $this->edit($module, json_decode($_POST[updated],true));
        }
        if(isset($_POST[deleted])){
            $this->del($module, json_decode($_POST[deleted],true));
        }
                
        $result['statuscode'] = 0;
        $result['statusmessage'] = L('SAVE_TIP');
        $this->ajaxReturn($result, 'json');
    }

    protected function add($module,$data)
    {
        foreach ($data as $value) {
            while (list($key, $val) = each($value)) {
                if($key != 'id') $inserted[$key] = $val;
            }
            $module->add($inserted);
        }
    }

    protected function edit($module,$data)
    {
        foreach ($data as $value) {
            $id = $value[id];
            while (list($key, $val) = each($value)) {
                if($key!='id'){
                    $updated[$key] = $val;
                }
            }
            $module->where('id='.$id)->setField($updated);
        }
    }

    protected function del($module,$data)
    {
        foreach ($data as $value) {
            $id = $value[id];
            $module->where('id='.$id)->delete();
        }
    }

    public function getAllTable()
    {
        $sql = "select TABLE_NAME as `text`,upper(left(TABLE_NAME,1))as `group` from information_schema.`TABLES` where TABLE_SCHEMA='db_oa' order by TABLE_NAME";
        $module = M();
        $data = $module->query($sql);
        $this->ajaxReturn($data, 'json');
    }

    public function import()
    {
        $tbl = $_POST[tbl_name];
        $sql = "insert into sys_langtable(tbl_name,fld_name)".
            "select TABLE_NAME,COLUMN_NAME from information_schema.`COLUMNS` ".
            "where TABLE_NAME='$tbl' ".
            "and COLUMN_NAME not in(select fld_name from sys_langtable where tbl_name='$tbl')";
        $module = M();
        $module->execute($sql);

        $result['statuscode'] = 0;
        $result['statusmessage'] = L('SAVE_TIP');
        $this->ajaxReturn($result, 'json');
    }
}